package com.qsmx.store.app.main.mem;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.fustion.FustionTag;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.mem.entity.MemManagerEntity;
import com.qsmx.store.app.util.StringUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import butterknife.BindView;
import butterknife.OnClick;

public class MemCardManagerBaseInfoDelegate extends AtmosDelegate {

    private MemManagerEntity memManagerEntity;

    @BindView(R2.id.et_mem_name)
    AppCompatEditText etMemName;
    @BindView(R2.id.et_mem_sex)
    AppCompatEditText etMemSex;
    @BindView(R2.id.et_mem_person_num)
    AppCompatEditText etMemPersonNum;
    @BindView(R2.id.et_mem_phone_num)
    AppCompatEditText etMemPhoneNum;
    @BindView(R2.id.et_mem_birthday_year)
    AppCompatEditText etMemBirthdayYear;
    @BindView(R2.id.et_mem_birthday_month)
    AppCompatEditText etMemBirthdayMonth;
    @BindView(R2.id.et_mem_birthday_day)
    AppCompatEditText etMemBirthdayDay;

    @OnClick(R2.id.tv_sure_set)
    void onSureSet() {
        if (checkAllIsNotInput()) {
            setBaseInfo(memManagerEntity.getMemberNo(),
                    etMemName.getText().toString(),
                    etMemSex.getText().toString(),
                    etMemBirthdayYear.getText().toString() + "." + etMemBirthdayMonth.getText().toString() + "." + etMemBirthdayDay.getText().toString(),
                    etMemPersonNum.getText().toString(),
                    etMemPhoneNum.getText().toString());
        } else {
            showShortToast("请填写需要修改的信息！");
        }
    }

    public static MemCardManagerBaseInfoDelegate create(MemManagerEntity memManagerData) {

        final Bundle args = new Bundle();
        args.putSerializable(FustionTag.MEM_CARD_INFO, memManagerData);

        final MemCardManagerBaseInfoDelegate delegate = new MemCardManagerBaseInfoDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            memManagerEntity = (MemManagerEntity) args.getSerializable(FustionTag.MEM_CARD_INFO);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_mem_card_manager_base_info;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {


    }

    private boolean checkAllIsNotInput() {
        if (StringUtil.isNotEmpty(etMemName.getText().toString()) ||
                StringUtil.isNotEmpty(etMemSex.getText().toString()) ||
                StringUtil.isNotEmpty(etMemPersonNum.getText().toString()) ||
                StringUtil.isNotEmpty(etMemPhoneNum.getText().toString()) ||
                StringUtil.isNotEmpty(etMemBirthdayYear.getText().toString()) ||
                StringUtil.isNotEmpty(etMemBirthdayMonth.getText().toString()) ||
                StringUtil.isNotEmpty(etMemBirthdayDay.getText().toString())) {
            return true;
        } else {
            return false;
        }
    }

    private void setBaseInfo(String memberNo, String memberName, String memberSex, String memberBirthday, String memberIdCard, String memberPhone) {
        JSONObject params = new JSONObject();
        params.put("deptId", FustionManager.mMachineData.getDeptId());
        params.put("memberNo", memberNo);
        if (StringUtil.isNotEmpty(memberName)) {
            params.put("memberName", memberName);
        } else {
            params.put("memberName", memManagerEntity.getMemberName());
        }
        if (StringUtil.isNotEmpty(memberSex)) {
            params.put("memberSex", memberSex);
        } else {
            params.put("memberSex", memManagerEntity.getMemberSex());
        }
        if (StringUtil.isNotEmpty(memberBirthday)) {
            params.put("memberBirthday", memberBirthday);
        } else {
            params.put("memberBirthday", memManagerEntity.getMemberBirthday());
        }
        if (StringUtil.isNotEmpty(memberIdCard)) {
            params.put("memberIdCard", memberIdCard);
        } else {
            params.put("memberIdCard", memManagerEntity.getMemberIdCard());
        }
        if (StringUtil.isNotEmpty(memberPhone)) {
            params.put("memberPhone", memberPhone);
        } else {
            params.put("memberPhone", memManagerEntity.getMemberPhone());
        }

        HttpHelper.RestClientPostRaw(params, HttpUrl.MODIFY_MEMBER_BASIC_INFO,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });
    }
}
